jQuery(function() {
    var _lengthwayTable = jQuery('#lengthwayTable');
    var _crosswiseTables = jQuery('#crosswiseTables');
    var _itemTables = jQuery('#itemTables');
    var _lieTablers = jQuery('#lieTables');
    var _baiLeft = jQuery('#bai_left');
    var _baiRight = jQuery('#bai_right');
    var _body = jQuery('body');
    var _selectBox = [];
    jQuery('span.drop-years-menu').on('click', function() {
        var _this = jQuery(this);
        var _opertor = jQuery('dd.years-list');
        var _status = _opertor.attr('data-open');
        if (_status == 'off') {
            _opertor.slideDown();
            _opertor.attr('data-open', 'on');
            _this.removeClass('icon-chevron-down').addClass('icon-chevron-up');
        } else {
            _opertor.slideUp();
            _opertor.attr('data-open', 'off');
            _this.removeClass('icon-chevron-up').addClass('icon-chevron-down');
        }
    })
    jQuery('input.history').on('click', function() {
        var _this = jQuery(this);
        var _index = parseInt(_this.next().attr('data-index'));
        var _status = _this.next().attr('data-status');
        var _kjboxLi = jQuery('.kj-box li');
        if (_status == 'on') {
            if (jQuery.inArray(_index, _selectBox)) {
                _selectBox.push(_index);
            }
        } else {
            for (var i in _selectBox) {
                if (_selectBox[i] == _index) {
                    _selectBox.splice(i, 1);
                }
            }
        }
        sessionStorage.selectBox = JSON.stringify(_selectBox);
        _kjboxLi.each(function(i, elment) {
            var __this = jQuery(this);
            switch (_index) {
            case 1:
                if (_status == 'on') {
                    _this.next().attr('data-status', 'off');
                    __this.find('dt').hide();
                } else {
                    _this.next().attr('data-status', 'on');
                    __this.find('dt').show();
                }
                break;
            default:
                if (_status == 'on') {
                    _this.next().attr('data-status', 'off');
                    __this.find('dd:eq(' + (_index - 2) + ')').hide();
                } else {
                    _this.next().attr('data-status', 'on');
                    __this.find('dd:eq(' + (_index - 2) + ')').show();
                }
            }
        })
    })
    jQuery('input[name=zoushi],input[name=history]').on('click', function() {
        var _url = jQuery(this).next().attr('href');
        window.location.href = _url;
    })
    jQuery('a.history').on('click', function() {
        var _this = jQuery(this);
        _this.prev().click();
    })
    jQuery(window).resize(function() {
        autoHeight();
        autoWidth();
        autoScroll();
    });
    function autoHeight() {
        var _height = jQuery('.menu').height();
        jQuery('.gexian').css({
            marginTop: _height + 'px'
        });
    }
    function autoWidth() {
        var _width = jQuery(window).width();
        var _commonWidth = ((_width - 820) / 2);
        if (_width > 820) {
            _lengthwayTable.css({
                left: _commonWidth + "px"
            })
            _crosswiseTables.css({
                left: _commonWidth + "px"
            })
            _lieTablers.css({
                left: _commonWidth + "px"
            })
            _baiLeft.css({
                width: _commonWidth + 'px'
            });
            _baiRight.css({
                width: _commonWidth + 'px'
            });
        } else {
            _lengthwayTable.css({
                left: "0px"
            })
            _crosswiseTables.css({
                left: "0px"
            })
            _lieTablers.css({
                left: "0px"
            })
            _baiLeft.css({
                width: '0px'
            });
            _baiRight.css({
                width: '0px'
            });
        }
        _lengthwayTable.show();
        _crosswiseTables.show();
        _lieTablers.show();
    }
    function autoScroll() {
        var _height = jQuery('.menu').height();
        var _width = jQuery(window).width();
        var _commonWidth;
        if (_width > 820) {
            _commonWidth = ((_width - 820) / 2);
        } else {
            _commonWidth = 0;
        }
        _lengthwayTable.css({
            top: _height + 'px'
        });
        _crosswiseTables.width(_itemTables.width());
        var _itemTablesTheadTds = _itemTables.find('thead').find('td');
        var _crosswiseTablesTheadTds = _crosswiseTables.find('thead').find('td');
        var _lengthwayTableTheadTds = _lengthwayTable.find('thead').find('td');
        var _itemTablesTrFirstTds = _itemTables.find('tr:first').find('td');
        var _widths = 0;
        var count = _lengthwayTableTheadTds.length;
        _itemTablesTrFirstTds.each(function(i) {
            if (i < count)
                _widths = _widths + jQuery(this).outerWidth();
        })
        _lengthwayTable.width(_widths + 1);
        _lengthwayTableTheadTds.each(function(i) {
            jQuery(this).width(jQuery(_itemTablesTheadTds[i]).width());
        })
        _crosswiseTablesTheadTds.each(function(i) {
            jQuery(this).width(jQuery(_itemTablesTheadTds[i]).width() + 1);
        })
        _lieTablers.width(jQuery(_itemTablesTheadTds[0]).outerWidth() + 1);
        jQuery(window).scroll(function() {
            var _this = jQuery(this);
            var _scrollTop = _this.scrollTop() - _height;
            _lengthwayTable.css({
                top: '-' + _scrollTop + 'px'
            });
        });
        jQuery('body').scroll(function() {
            var _this = jQuery(this);
            var _scrollLeft = (_commonWidth - _this.scrollLeft());
            _crosswiseTables.css({
                left: _scrollLeft + 'px'
            });
        });
    }
    function load() {
        document.addEventListener('touchstart', touch, false);
        document.addEventListener('touchmove', touch, false);
        document.addEventListener('touchend', touch, false);
        function autoMove(_offsetLeft) {
            if (_itemTables.length > 0) {
                _offsetLeft = _itemTables.offset().left;
                console.log('touch:' + _offsetLeft);
                _crosswiseTables.css({
                    left: _offsetLeft + 'px'
                });
            }
        }
        function touch(event) {
            var event = event || window.event;
            var _offsetLeft = 0;
            var _scrollLeft = _body.scrollLeft();
            if (_scrollLeft > 0) {} else {
                autoMove(_offsetLeft);
                switch (event.type) {
                case "touchstart":
                    break;
                case "touchend":
                    setTimeout(function() {
                        autoMove(_offsetLeft);
                    }, 600)
                    break;
                case "touchmove":
                    break;
                }
            }
        }
    }
    function showPage() {
        jQuery('#topbartit').find('.loading').hide();
        jQuery('#whiteBox').css({
            opacity: 1
        })
    }
    autoHeight();
    autoWidth();
    autoScroll();
    window.addEventListener('load', load, false);
    if (sessionStorage.selectBox == undefined) {
        showPage();
    } else {
        var __sessionSelectBox = JSON.parse(sessionStorage.selectBox);
        if (__sessionSelectBox.length > 0) {
            jQuery('a.history').each(function() {
                var __this = jQuery(this);
                var __index = parseInt(__this.attr('data-index'));
                for (var i in __sessionSelectBox) {
                    if (__index == __sessionSelectBox[i]) {
                        __this.click();
                    }
                }
            })
        }
        showPage();
    }
});
